#!/bin/bash
function green(){
    echo -e "\033[32m\033[01m $1 \033[0m"
}

#save the data
cp -r jupyter/root ~ 2>/dev/null

#enter home and create jupyter dir
cd $HOME & rm -rf jupyter && mkdir jupyter
cd jupyter
#openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout mykey.key -out mycert.pem
#curl https://get.acme.sh | sh
basedomian='whxukang.cn'

#make sure python3 is installed
#create virtualenv and install jupyter
python3 -m venv venv
source venv/bin/activate
pip install wheel jupyter

#create project rootdir
mkdir root

#create pw
pw=$(python3 -c "from notebook.auth import passwd;print(passwd())")

~/.acme.sh/acme.sh --installcert -d $basedomian \
        --key-file   $HOME/jupyter/privkey.pem \
        --fullchain-file $HOME/jupyter/fullchain.pem

#create config
jupyter notebook --generate-config --allow-root -y
cat >> $HOME/.jupyter/jupyter_notebook_config.py<<-EOF
c.NotebookApp.certfile = u'$HOME/jupyter/fullchain.pem'
c.NotebookApp.keyfile = u'$HOME/jupyter/privkey.pem'
c.NotebookApp.ip = '*'
c.NotebookApp.password = u'$pw'
c.NotebookApp.open_browser = False
c.NotebookApp.port = 8888
c.NotebookApp.allow_root = True
c.NotebookApp.notebook_dir = u'$HOME/jupyter/root'
EOF

#other extensions
pip install jupyter_contrib_nbextensions
jupyter contrib nbextension install --user
pip install jupyter_nbextensions_configurator
jupyter nbextensions_configurator enable --user

#create links
rm -rf /usr/local/bin/jupyter
ln -s $HOME/jupyter/venv/bin/jupyter /usr/local/bin/jupyter

#recover the data
cp -r ~/root jupyter/ 2>/dev/null

# fix some error
sudo pip install nbconvert==5.6.1
sudo pip install jupyter-client==6.1.12

#install analysis packages (seaborn includes numpy pandas matplotlib scipy)
pip install openpyxl xlrd seaborn statsmodels pyecharts plotly mlxtend

#create service
systemctl stop jupyter
cat >/usr/lib/systemd/system/jupyter.service<<-EOF
[Unit]
Description=jupyter

[Service]
ExecStart=$HOME/jupyter/venv/bin/jupyter notebook

[Install]
WantedBy=multi-user.target
EOF
systemctl enable jupyter
systemctl daemon-reload
systemctl start jupyter

# nginx configuration
green "===input the prefix of domain==="
read prefix
domain=$prefix.$basedomian


# client_max_body_size     50m; //set the entity body size to 50M
cat >/etc/nginx/conf.d/jupyter.conf<<-EOF
server {
        listen 443 ssl;
        server_name $domain;
        ssl_certificate  $HOME/jupyter/fullchain.pem;
        ssl_certificate_key $HOME/jupyter/privkey.pem;
        ssl_session_timeout 5m;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;
        add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;

        client_max_body_size     50m;
        client_header_timeout    1m;
        client_body_timeout      1m;
        location / {
                proxy_pass https://127.0.0.1:8888;
                proxy_set_header Host \$host;
                # websocket support
                proxy_http_version 1.1;
                proxy_set_header Upgrade "websocket";
                proxy_set_header Connection "Upgrade";
        }
}

EOF
nginx -s reload
systemctl stop nginx
systemctl start nginx
